﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using MapCenterSystem.DataInfo;
using System.Data.SqlClient;
using MapCenterSystem.Configuration;

namespace MapCenterSystem.DataAccess
{
    public class PlaceTypeServices
    {
        public PlaceTypeServices()
        { }
        public PlaceTypeInfo GetPlaceTypeInfo(int placeType)
        {
            using (SqlConnection sqlConnect = new SqlConnection(GlobalConfig.ConnectionString))
            {
                SqlCommand command = new SqlCommand();
                command.CommandText = "sp_PlaceType_GetPlaceTypeInfo";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = sqlConnect;
                command.CommandTimeout = GlobalConfig.TimeOut;
                command.Parameters.AddWithValue("@id", placeType);
                try
                {
                    sqlConnect.Open();
                }
                catch
                {
                    throw new Exception("Can not connect to database. Please check connect");
                }
                SqlDataReader reader = command.ExecuteReader();
                PlaceTypeInfo placeTypeInfo = new PlaceTypeInfo();
                while (reader.Read())
                {
                    placeTypeInfo.ID = placeType;
                    placeTypeInfo.Description = Convert.ToString(reader["description"]);
                    placeTypeInfo.Picture = Convert.ToString(reader["picture"]);
                }
                return placeTypeInfo;
            }
        }

        public IList<PlaceTypeInfo> GetListPlaceType()
        {
            using (SqlConnection sqlConnect = new SqlConnection(GlobalConfig.ConnectionString))
            {
                SqlCommand command = new SqlCommand();
                command.CommandText = "sp_PlaceType_GetListPlaceType";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = sqlConnect;
                command.CommandTimeout = GlobalConfig.TimeOut;
                try
                {
                    sqlConnect.Open();
                }
                catch
                {
                    throw new Exception("Can not connect to database. Please check connect");
                }
                SqlDataReader reader = command.ExecuteReader();
                IList<PlaceTypeInfo> result = new List<PlaceTypeInfo>();
                while (reader.Read())
                {
                    int placeType = Convert.ToInt32(reader["id"]);
                    PlaceTypeInfo placeTypeInfo = GetPlaceTypeInfo(placeType);
                    result.Add(placeTypeInfo);
                }
                return result;
            }
        }
    }
}
